﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Gatheshop.DBUtility;
using System.Data;

namespace Gatheshop.DAL
{
     public partial class gift_card
    {
        public gift_card()
        { }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public Boolean deleteGiftCard(string id)
        {
            Boolean isSucess = false;
            try
            {
                
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update ges_gift_card set isDel='y' ");
                strSql.Append(" where id=" + id);
                DbHelperSQL.ExecuteSql(strSql.ToString());
                isSucess = true;
                return isSucess;
            }
            catch (Exception ex)
            {
                throw ex;
               
            }
            
        }

        public DataTable GetAllCard()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from ges_gift_card where isDel='n' ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DataTable dt = ds.Tables[0] as DataTable;
            return dt;
        }
        public int getMaxCardNo()
        {
            int maxCardNo =0;
            string str = "select max(id) as id from ges_gift_card ";
            DataSet ds = DbHelperSQL.Query(str.ToString());
            DataTable dt = ds.Tables[0] as DataTable;
            if ((dt.Rows.Count == 1)&&(dt.Rows[0]["id"].ToString()!=""))
            {
                maxCardNo = Convert.ToInt32(dt.Rows[0]["id"].ToString());
            }
                return maxCardNo;
        }
         //未激活的卡金额
        public Decimal getCardCount(string cardNo, string cardPwd)
        {
            string str = "select count from ges_gift_card where isDel='n' and isActive='n' and cardNo='"+cardNo+"' and cardPwd='"+cardPwd+"'";
            DataSet ds = DbHelperSQL.Query(str.ToString());
            DataTable dt = ds.Tables[0] as DataTable;
            Decimal count = 0;
            if (dt.Rows.Count != 0)
            {

count= Convert.ToDecimal(dt.Rows[0]["count"].ToString());
            }
            return count;
        }
        public Boolean setActive(string cardNo, string cardPwd,string owner)
        {
            Boolean isSucess = false;
            try
            {
                string activeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update ges_gift_card set isActive='y',owner='"+owner+"',activeTime='"+activeTime+"'");
                strSql.Append(" where cardNo='" + cardNo + "' and cardPwd='" + cardPwd + "'");
                DbHelperSQL.ExecuteSql(strSql.ToString());
                isSucess = true;
                return isSucess;
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }

        public Boolean addGiftCard(string cardNo, string cardPwd, int count, string createTime)
        {
            //insert into ges_gift_card(cardNo,cardPwd,count,createTime,isActive,isDel)
//values(0000000008,'aaaaaaaaa',300,'2013-08-05 18:45:38','n','n')
            Boolean isSucess = false;
            try
            {

                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into ges_gift_card(cardNo,cardPwd,count,createTime,isActive,isDel) ");
                strSql.Append(" values('" + cardNo + "','" + cardPwd + "'," + count + ",'" + createTime + "','n','n')");
                DbHelperSQL.ExecuteSql(strSql.ToString());
                isSucess = true;
                return isSucess;
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }
    }
}
